import { flysel } from "../plugin/flysel.js";
import { hideBar } from "./scroll-bar.js";
import { touch } from "./tool.js";

const { html } = flysel

const childwin = flysel('#childwin')

if (touch) childwin.addClass('brief')

class Alter {
    boxE
    type
    closeE
    button = new Map
    /**弹出时触发 */
    oneject
    constructor(type, option) {
        this.type = type
        this.boxE = childwin.find(`[type="${type}"]`)
        this.closeE = this.boxE.find('.icon-close').on('click', () => {
            this.close()
        })
        if (option?.button) Object.entries(option.button).forEach(a => this.button.set(...a))
        this.oneject = option?.oneject
        const alter = this
        this.boxE.find('.tail').on('click', function () {
            const type = this.e.classList.item(0)
            alter.button.get(type)?.call(alter, ...alter.ejectProp)
        }, {
            entrust: 'button'
        })
        Alter.list.set(type, this)
        option?.oninit?.call(this)
    }
    eject() {
        hideBar()
        this.oneject && this.oneject(...arguments)
        this.ejectProp = [...arguments]
        html.css('overflowY', 'hidden')
        childwin.flex()
        this.boxE.addClass('open')
    }
    close() {
        html.css('overflowY', '')
        childwin.hide()
        this.boxE.removeClass('open')
        delete this.ejectProp
    }
    state() {
        return this.boxE.hasClass('open')
    }
    static list = new Map
}

new Alter('alter', {
    oneject(text, title) {
        const { boxE } = this
        boxE.find('.text').text = text
    },
    button: {
        confirm() {
            this.close()
            this.ejectProp && this.ejectProp[1]?.call(this)
        }
    }
})
new Alter('confirm', {
    oninit() {

    },
    oneject(text) {
        const { boxE } = this
        boxE.find('.text').text = text
    },
    button: {
        confirm(t, f) {
            f && f()
            this.close()
        },
        cancel() {
            this.close()
        }
    }
})
new Alter('custom')
new Alter('style', {
    oninit() {
        const sidebar = flysel('#sidebar')
        flysel('#expand-sidebar').on('click', () => {
            sidebar.removeClass('hide')
        })
    },
    button: {
        cancel(f) {
            f && f()
            this.close()
        },
        confirm() {
            this.close()
        }
    }
})

childwin.on('click', function () {
    close(this.parent('.win').attr('type'))
}, {
    entrust: '.icon-close'
})

export function eject(win, ...prop) {
    Alter.list.get(win)?.eject(...prop)
}

export function close(win) {
    Alter.list.get(win)?.close()
}